<template>
	<view class="mine-container" :style="{height: `${windowHeight}px`}">
		<!--顶部个人信息栏-->
		<view class="header-section">
			<view class="flex padding justify-between">
				<view class="flex align-center">
					<view v-if="!avatar" class="cu-avatar xl round bg-white">
						<view class="iconfont icon-people text-gray icon"></view>
					</view>
					<image v-if="avatar" @click="handleToAvatar" :src="avatar" class="cu-avatar xl round"
						mode="widthFix">
					</image>
					<view v-if="!name" @click="handleToLogin" class="login-tip">
						点击登录
					</view>
					<view v-if="name" @click="handleToInfo" class="user-info">
						<view class="u_title">
							用户名：{{ name }}
						</view>
					</view>
				</view>
				<view @click="handleToInfo" class="flex align-center">
					<text>个人信息</text>
					<view class="iconfont icon-right"></view>
				</view>
			</view>
		</view>
		<view class="menu-list">
			<view class="list-cell list-cell-arrow" @click="handleToEditInfo">
				<view class="menu-item-box">
					<view class="iconfont icon-user menu-icon"></view>
					<view>编辑资料</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleToPwd">
				<view class="menu-item-box">
					<view class="iconfont icon-password menu-icon"></view>
					<view>修改密码</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="upgrade">
				<view class="menu-item-box">
					<view class="iconfont icon-refresh menu-icon"></view>
					<view>检查更新</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleCleanTmp">
				<view class="menu-item-box">
					<view class="iconfont icon-clean menu-icon"></view>
					<view>清理缓存:{{fileSizeString}}</view>
				</view>
			</view>
		</view>
		<view class="cu-list menu">
			<view class="cu-item item-box">
				<view class="content text-center" @click="handleLogout">
					<text class="text-black">退出登录</text>
				</view>
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
	import checkVersion from '@/pages/lq-upgrade/checkVersion.js'
	import {
		cheakFile
	} from '@/api/system/cheakload.js'
	import config from '@/config'
	export default {
		data() {
			return {
				name: this.$store.state.user.nickName,
				version: getApp().globalData.config.appInfo.version,
				fileSizeString:""
			}
		},
		computed: {
			avatar() {
				return this.$store.state.user.avatar
			},
			windowHeight() {
				return uni.getSystemInfoSync().windowHeight - 50
			}
		},
		created() {
			this.formatSize();
		},
		methods: {
			handleToInfo() {
				this.$tab.navigateTo('/pages/mine/info/index')
			},
			handleToEditInfo() {
				this.$tab.navigateTo('/pages/mine/info/edit')
			},
			handleToLogin() {
				this.$tab.reLaunch('/pages/login')
			},
			handleToAvatar() {
				this.$tab.navigateTo('/pages/mine/avatar/index')
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			},
			async upgrade() {
				var a = {}
				cheakFile({ state: 'Y'}).then(res => {
					// 获取系统信息
					var version = uni.getStorageSync('version');
					var versionCode = uni.getStorageSync('versionCode');
					console.log(versionCode)
					if (res.length > 0) {
						if (versionCode < Number(res[0].versionName)) {
							let name = res[0].versionNumber; //新版本名称
							let code = Number(res[0].versionName); //新版本号（数字）
							let content = res[0].content; //更新内容
							let url = config.baseUrl + "/system/file/download?fileName=" + res[0]
								.fileUrl; //app下载链接，（演示请换成真正在下载链接）
							//let url ="http://60.205.104.240:8080/common/download?fileName=UNI20220706200750.apk&delete=false"
						
							let forceUpdate = true; //是否强制更新
							a = {
								name, //最新版本名称
								code, //最新版本号
								content, //更新内容
								url, //下载链接
								forceUpdate //是否强制升级			  
							}
								console.log(a)
							checkVersion(a)
						} else {
							uni.showToast({
								title: '暂无更新',
								icon: 'none',
								duration: 2000,
							})
						}
					} else {
						uni.showToast({
							title: '暂无更新',
							icon: 'none',
							duration: 2000,
						})
					}
			
				}).catch(error => {
					reject(error)
				})
			
			},
			handleBuilding() {
				this.$modal.showToast('模块建设中~')
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			},
			// 获取缓存
			formatSize() {
				plus.cache.calculate((size) => {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						this.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						this.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						this.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						this.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			handleToPwd() {
				this.$tab.navigateTo('/pages/mine/pwd/index')
			},
			handleCleanTmp() {
				let that = this; 
				uni.showModal({
					title: '清除缓存',
					content: '您确定要清除缓存吗？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							that.clearCache();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			clearCache() {
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i];
						plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
							if (entry.isDirectory) {
								entry.removeRecursively((entry) => {
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									this.formatSize();
								}, (e) => {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, (e) => {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(() => {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						this.formatSize();
					});
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f6f7;
	}
	.item-box {
		background-color: #FFFFFF;
		margin: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
		border-radius: 8rpx;
		color: #303133;
		font-size: 32rpx;
	}
	.mine-container {
		width: 100%;
		height: 100%;


		.header-section {
			padding: 15px 15px 45px 15px;
			background-color: #46a3ff;
			color: white;

			.login-tip {
				font-size: 18px;
				margin-left: 10px;
			}

			.cu-avatar {
				border: 2px solid #eaeaea;

				.icon {
					font-size: 40px;
				}
			}

			.user-info {
				margin-left: 15px;

				.u_title {
					font-size: 18px;
					line-height: 30px;
				}
			}
		}

		.content-section {
			position: relative;
			top: -50px;

			.mine-actions {
				margin: 15px 15px;
				padding: 20px 0px;
				border-radius: 8px;
				background-color: white;

				.action-item {
					.icon {
						font-size: 28px;
					}

					.text {
						display: block;
						font-size: 13px;
						margin: 8px 0px;
					}
				}
			}
		}
	}
</style>